package com.app.couponsharelife.base

import android.app.ProgressDialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.app.couponsharelife.utils.ToastUtil

/**
 *Created by xct on 2019/1/2
 *Description:
 */
abstract class BaseFragment:Fragment(),IBaseView {
    var mViewDataBinding: ViewDataBinding? = null
    /**
     * 进度对话框
     */
    private var mProgressDialog: ProgressDialog? = null
    /**
     * 绑定布局文件
     *
     * @return 布局文件ID
     */
    abstract val viewLayoutId: Int

    /**
     * 初始化视图控件
     */
    protected abstract fun initViews()

    /**
     * 当视图初始化并且对用户可见的时候去真正的加载数据
     */
    protected abstract fun lazyLoad()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        /*if (mViewDataBinding == null) {
            mViewDataBinding = DataBindingUtil.inflate(inflater, viewLayoutId, container, false)
        }
        val parent = mViewDataBinding!!.root.parent
        if (parent != null) {
            (parent as ViewGroup).removeView(mViewDataBinding!!.root)
        }*/
        return inflater.inflate(viewLayoutId,container,false)
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
    }
    override fun showProgressDialog(msg: String) {
        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog(context)
            mProgressDialog!!.setCancelable(false)
            mProgressDialog!!.setProgressStyle(ProgressDialog.STYLE_SPINNER)
        }
        mProgressDialog!!.setMessage(msg)
        mProgressDialog!!.show()
    }

    override fun dismissProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog!!.dismiss()
        }
    }
    /**
     * 弹出Toast
     */
    override fun showToastMsg(msg: String) {
        ToastUtil.getInstance().showToast(msg)
    }
}